package com.euond.permission.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.euond.common.core.dto.ResultDto;
import com.euond.common.core.exception.ServiceException;
import com.euond.common.core.service.RedisService;
import com.euond.common.core.service.impl.BaseServiceImpl;
import com.euond.permission.dto.DataPrivilegeDtlDto;
import com.euond.permission.entities.DataPrivilegeDtl;
import com.euond.permission.mapper.DataPrivilegeDtlMapper;
import com.euond.permission.service.DataPrivilegeDtlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by user on 2018/1/6.
 */
@Service
public class DataPrivilegeDtlServiceImpl extends BaseServiceImpl<DataPrivilegeDtlMapper,DataPrivilegeDtl> implements DataPrivilegeDtlService {

    @Autowired
    private DataPrivilegeDtlMapper dataPrivilegeDtlMapper;
    @Autowired
    private RedisService redisService;
    private static Long redis_key_exp = Long.valueOf(7200L);

    /**
     * 根据权限配置ID查询权限配置明细
     * @param searchParams
     * @return
     */
    public ResultDto findDataPrivilegeDtlByPrivilegeId(Map<String,Object> searchParams){
        int pageCurrent=1;
        int pageSize=10;
        Long privilegeId=null;

        for (Map.Entry<String, Object> entry : searchParams.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();

            if (key.equals("pageCurrent")) {
                pageCurrent = Integer.parseInt(value.toString());
            }
            if (key.equals("pageSize")) {
                pageSize = Integer.parseInt(value.toString());
            }
            if (key.equals("privilegeId")) {
                privilegeId=Long.parseLong(value.toString());
            }

        }
        if(privilegeId.equals(null)){
            throw new ServiceException("查询参数不正确");
        }
        Page<DataPrivilegeDtl> page=new Page(pageCurrent,pageSize);
        List<DataPrivilegeDtl> dataPrivilegeDtlList=dataPrivilegeDtlMapper.findDataPrivilegeDtlByPrivilegeId(page,privilegeId);
        page.setRecords(dataPrivilegeDtlList);
        ResultDto resultDto=new ResultDto("0","seccess",page.getRecords(),page.getTotal());
        return resultDto;

    }

    /**
     * 根据权限配置ID删除权限配置明细
     * @param privilegeId
     * @return
     */
    @Transactional
    public void deleteDataPrivilegeDtlByPrivilegeId(long privilegeId){
        EntityWrapper entityWrapper=new EntityWrapper();
        entityWrapper.eq("privilege_id",privilegeId);
        delete(entityWrapper);
    }

    /**
     * 根据权限配置ID查询所有的资源(url)
     * @param privilegeId
     * @return
     */
    public String findDataPrivilegeDtlResource(long privilegeId){
        String resourceList="";
        List<DataPrivilegeDtlDto> dataPrivilegeDtlDtoList=dataPrivilegeDtlMapper.findDataPrivilegeDtlResource(privilegeId);
        for(int i=0;i<dataPrivilegeDtlDtoList.size();i++){
            resourceList=resourceList+","+dataPrivilegeDtlDtoList.get(i).getResourceUrl();
        }
        return resourceList;
    }

    /**
     * 查询数据权限需要的数据
     * @param privilegeId,resourceUrl
     * @return
     */
    public List<DataPrivilegeDtlDto> findDataPrivilegeDtlDto(Long privilegeId, String resourceUrl){
        List<DataPrivilegeDtlDto> DataPrivilegeDtlDto=null;
        if(this.redisService != null) {
            Object obj = this.redisService.get("DataPrivilegeDtlDto_" + privilegeId + "_" + resourceUrl);
            if(obj != null) {
                DataPrivilegeDtlDto = (List)obj;
            }
        }
        if(DataPrivilegeDtlDto!=null){
            DataPrivilegeDtlDto=dataPrivilegeDtlMapper.findDataPrivilegeDtlDto(privilegeId,resourceUrl);

            if(this.redisService != null) {
                this.redisService.set("DataPrivilegeDtlDto_" + privilegeId + "_" + resourceUrl, DataPrivilegeDtlDto, redis_key_exp);
            }
        }
        return DataPrivilegeDtlDto;
    }

}
